﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRoomInspection.Properties;
using YJKRoomInspection.ShowDataForm;
using YJKRoomInspection.Utiltiy;
using YJKRevitBase.WinForm;
using YJKInspectModelFunc;

namespace YJKRoomInspection.ProgramArchitecture
{
	internal static class TempeltCmdClass
	{
		public static void Execute(string cmdName, ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			try
			{
				if (YJKInspectModelFunc.Utility.IfEnvironmentCanUse(commandData))
				{
					bool tmpTag = false;
					if (cmdName.Equals(Resources.Str_SurfaceCheck))
					{
						tmpTag = true;
					}
					UIApplication application = commandData.Application;
                    Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
					Document document = application.ActiveUIDocument.Document;
					StaticRescource.CmdData = commandData;
					string appRootPath = YJKRoomInspection.Utiltiy.Utility.GetAppRootPath();
					string versionNumber = application.Application.VersionNumber;
					StaticRescource.ParamXmlPath = appRootPath + "\\Data\\";
					IntPtr mainWindowHandle = Process.GetCurrentProcess().MainWindowHandle;
                    StaticRescource.m_hWndRevit = new WindowHandle(mainWindowHandle);
					StaticRescource.IfHasUse = true;
					List<Level> list = new List<Level>();
					bool flag = true;
					SelectFloorManager selectFloorManager = new SelectFloorManager();
					if (selectFloorManager.SelectFloorLevels(document, tmpTag))
					{
						list = selectFloorManager.m_AllLevels;
						StaticRescource.m_SelectLevels = list;
						flag = selectFloorManager.IfYJKRoomInspection;
						FrontRoomAppendCheckManger frontRoomAppendCheckManger = null;
						try
						{
							frontRoomAppendCheckManger = new FrontRoomAppendCheckManger(cmdName, document, list);
						}
						catch (Exception)
						{
							frontRoomAppendCheckManger = null;
						}
						if (flag)
						{
							PublicYJKRoomInspection publicYJKRoomInspection = new PublicYJKRoomInspection(commandData, list, new WindowHandle(mainWindowHandle));
							publicYJKRoomInspection.DoCheck();
							if (publicYJKRoomInspection.IfManualAbosrt)
							{
								YJKInspectModelFunc.Utility.DeleteAddRoom(document, new List<int>());
								return;
							}
						}
						if (frontRoomAppendCheckManger != null)
						{
							frontRoomAppendCheckManger.CheckMethod();
						}
					}
				}
			}
			catch (Exception)
			{
				MessageBox.Show("出错");
			}
		}
	}
}
